<?php
namespace MyProject\EhrBundle\DataFixtures\ORM;

use Doctrine\Common\DataFixtures\FixtureInterface,
    My\EhrBundle\Entity\Personnel,
    My\EhrBundle\Entity\PersonnelAddress,
    My\EhrBundle\Entity\PersonnelBankAccount,
    My\EhrBundle\Entity\PersonnelContactInfo,
    My\EhrBundle\Entity\PersonnelEducation,
    My\EhrBundle\Entity\PersonnelEmployementInfo,
    My\EhrBundle\Entity\PersonnelEmployementRecord,
    My\EhrBundle\Entity\PersonnelFamily,
    My\EhrBundle\Entity\PersonnelJobHistory,
    My\EhrBundle\Entity\PersonnelPayrollInfo,
    My\EhrBundle\Entity\PersonnelPersonalInfo,
    My\EhrBundle\Entity\PersonnelTrainingHistory,
    My\EhrBundle\Entity\MasterInstitution;

class PersonnelFixtures implements FixtureInterface
{
    private $_em = null;

    public function load($em)
    {
        $this->_em = $em;
        
        $u = 1;

        while( $u < 23 )
        {
            $this->_createPersonnel($em, $u);
            ++$u;

            echo "DONE CREATEING PERSONAL {$u}\r\n";
        }
        //$em->flush();
    }
    private function _createEmployementInfo( $em, $u = 1 )
    {
        $ei  = new PersonnelEmployementInfo();
        
        $employee_no = \preg_replace( '/\./','0', \sprintf("%4f", \floatval($u)) );
        
        $ei->setEmployeeNo( $employee_no );
        $ei->setHiringType( 'Pekerja Tetap');
        $ei->setWorkday( 'Full Time'  );
        $ei->setGrade( '07 - 08' );
        $ei->setTaxStatus( 'K'  );
        $ei->setPosition('Kepala Bagian ' . $u);
        $ei->setRemarks( ''  );
        $ei->setJoinDate( new \DateTime('1995-09-09')  );
        $ei->setMppDate( new \DateTime('1996-08-08'));
        $ei->setPensionDate( new \DateTime('2020-09-09')  );

        $em->persist( $ei );
        return $ei;
    }

    private function _createPersonalInfo( $em , $u = 1 )
    {
        $pi = new PersonnelPersonalInfo();
        $pi->setBloodType( $u % 2 == 0 ? 'B' : 'A' );
        $pi->setDob( new \DateTime( "197{$u}-01-01" ) );
        $pi->setPob( "Surakarta {$u}" );
        $pi->setGender( $u % 2 == 0 ? 'Pria' : 'Wanita' );
        $pi->setMaritalStatus( $u % 2 == 0 ? 'Belum Menikah' : 'Menikah');
        $pi->setName( 'Ir. Bob ' . ($u % 2 == 0 ? "Kertaradjasa 0{$u}" : "Mangku Kusuma 0{$u}") );
        $pi->setNoKtp( "000066779123{$u}" );
        $pi->setReligion( $u % 2 == 0 ? 'Satanist' : 'Budha' );
        $pi->setUploadKtp( 'gambar-ktp-' . md5( "000066779123{$u}" . \microtime()) .'.jpg' );
        $pi->setPhoto( 'gambar-foto-' . md5( "000066779123{$u}" . \microtime()) .'.jpg' );
        $em->persist( $pi );
        return $pi;
    }

    private function _createPersonnel( $em , $u )
    {
        $personnel = new Personnel();

        $personnel->setEmployementInfo( $this->_createEmployementInfo($em, $u));
        $personnel->setPersonalInfo($this->_createPersonalInfo($em, $u));

        $em->persist($personnel);
        $em->flush();

        AccountFixtures::createAccount( array( $personnel->getEmployementInfo()->getEmployeeNo(),'123', $personnel->getIdPersonnel(), 'Employee'), $em) ;

        $methods = explode(',', "Address,BankAccount,ContactInfo,Education,EmployementRecord,Family,JobHistory,PayrollInfo,TrainingHistory");

        foreach( $methods as $method)
        {
            $this->{"_create{$method}"}($personnel, 10);
        }
        
        $em->persist($personnel);
        $em->flush();
    }

    private function _createEducation( $personnel, $count =1 )
    {
        while( $count-- )
        {
            $edu = new PersonnelEducation();
            $edu->setEducationInstitution("Sample University {$count}");
            $edu->setEducationLevel("Education Level");
            $edu->setStartYear("1999");
            $edu->setEndYear("2010");
            $edu->setGpa('3.4');
            $edu->setIjazah('');
            $edu->setLocation('Amsterdam');
            $edu->setMajor('Sample Major');
            $edu->setPersonnel($personnel);

            $this->_em->persist( $edu );

            
        }
    }
    private function _createAddress( $personnel, $count = 1 )
    {
        while( $count-- )
        {
            $addr = new PersonnelAddress();
            $addr->setAddress("Jl. Cikini No {$count}");
            $addr->setCity('Tangerang');
            $addr->setFax('');
            $addr->setPhone('021-0228976' . $count);
            $addr->setZip("1415{$count}");
            $addr->setPersonnel($personnel);
            
            $this->_em->persist($addr);
        }


    }
    private function _createBankAccount( $personnel, $count = 1 )
    {
        while( $count-- )
        {
            $ba = new PersonnelBankAccount();
            $ba->setAccountNumber("004512349975{$count}");
            $ba->setBank('Bank Permata');
            $ba->setBranch('Cikokol Tangerang');
            $ba->setPersonnel($personnel);

            $this->_em->persist($ba);
        }
    }
    private function _createContactInfo( $personnel, $count = 1 )
    {
        while( $count-- )
        {
            $ci = new PersonnelContactInfo();
            $ci->setContact1('0213567781'.$count);
            $ci->setContactType('phone');
            $ci->setPersonnel($personnel);

            $this->_em->persist( $ci );
        }
    }
    private function _createEmployementRecord( $personnel, $count = 1 )
    {
        while( $count-- )
        {
            $er = new PersonnelEmployementRecord();
            $er->setAdmin("0{$count}");
            $er->setChangeType("Mutasi");
            $er->setGrade("0{$count} - 0{$count}");
            $er->setPosition("Supervisi PT Bintang Utama {$count}");
            $er->setTmtGrade( new \DateTime('now'));
            $er->setTmtPosition(new \DateTime('now'));
            $er->setPersonnel($personnel);

            $this->_em->persist( $er );
        }
    }
    private function _createFamily( $personnel, $count = 1 )
    {
        while( $count-- )
        {
            $fm = new PersonnelFamily();
            $fm->setRelationship("Anak Ke {$count}");
            $fm->setDob( new \DateTime('now'));
            $fm->setPob( "Semarang");
            $fm->setGender("Pria");
            $fm->setLastEducation("Sarjana");
            $fm->setName("Siti Julaeha {$count}");
            $fm->setPersonnel($personnel);

            $this->_em->persist($fm);
        }
    }
    private function _createJobHistory( $personnel, $count = 1 )
    {
        while( $count-- )
        {
            $jh = new PersonnelJobHistory();
            $jh->setCompany("PT Sinarmas {$count}");
            $jh->setEndDate(new \DateTime('now'));
            $jh->setStartDate(new \DateTime('2009-01-01'));
            $jh->setJobTitle("Kepala Bagian {$count}");
            $jh->setLocation("Surakarta");
            $jh->setPersonnel($personnel);


            $this->_em->persist($jh);
        }
    }
    private function _createPayrollInfo( $personnel, $count = 1 )
    {
        $pi = new PersonnelPayrollInfo();
        $pi->setAtasNamaBankAccount("Iskandar Zulkarnain {$count}");
        $pi->setBankAccount("Bank Mandiri {$count}");
        $pi->setBaseSalary("25,000,00{$count}");
        $pi->setNpwp("1,000,00{$count}");
        $pi->setPaymentType("TRANSFER");
        $pi->setPensionNo("000{$count}");
        $pi->setStartDate( new \DateTime('now'));
        $pi->setAttendence('4');
        $pi->setNumberOfDependent('4');
        $pi->setPersonnel($personnel);

        $this->_em->persist($pi);
    }
    private function _createTrainingHistory( $personnel, $count = 1 )
    {
        while( $count-- )
        {
            $th = new PersonnelTrainingHistory();
            $th->setDateStart( new \DateTime('now'));
            $th->setDateEnd( new \DateTime('now'));
            $th->setInstitution("Nama Institusi {$count}");
            $th->setTitle("Employement Strategy Course {$count}");
            $th->setLocation("Buaran City {$count}");
            $th->setDocument("sample-document-{$count}");


            $th->setPersonnel($personnel);

            $this->_em->persist( $th );
        }
    }


}
?>
